package com.example.myapplication2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

public class Activity_contact_detail extends AppCompatActivity {

    TextView textView,textView2;

    // 定义一个静态的Map，用于存储名称到URL的映射
    private static final Map<String, String> NAME_TO_URL_MAP = new HashMap<>();

    // 静态代码块，用于初始化NAME_TO_URL_MAP
    static {
        // 将每个名称与其对应的URL添加到Map中
        NAME_TO_URL_MAP.put("傲慢", "https://baike.baidu.com/item/%E5%82%B2%E6%85%A2/7238397");
        NAME_TO_URL_MAP.put("嫉妒", "https://baike.baidu.com/item/%E5%AB%89%E5%A6%92/18505");
        NAME_TO_URL_MAP.put("暴怒", "https://baike.baidu.com/item/%E6%9A%B4%E6%80%92/3299684");
        NAME_TO_URL_MAP.put("懒惰", "https://baike.baidu.com/item/%E6%87%92%E6%83%B0/826181");
        NAME_TO_URL_MAP.put("贪婪", "https://baike.baidu.com/item/%E8%B4%AA%E5%A9%AA/2751944");
        NAME_TO_URL_MAP.put("暴食", "https://baike.baidu.com/item/%E6%9A%B4%E9%A3%9F/1414843");
        NAME_TO_URL_MAP.put("色欲", "https://baike.baidu.com/item/%E8%89%B2%E6%AC%B2/3723346");
        NAME_TO_URL_MAP.put("堕落", "https://baike.baidu.com/item/%E5%A0%95%E8%90%BD/17031700");
        NAME_TO_URL_MAP.put("大逆", "https://baike.baidu.com/item/%E5%A4%A7%E9%80%86?fromModule=lemma_search-box");
        NAME_TO_URL_MAP.put("恶谋", "https://baike.baidu.com/");
        NAME_TO_URL_MAP.put("恋物", "https://baike.baidu.com/item/%E6%81%8B%E7%89%A9/3371212");
        NAME_TO_URL_MAP.put("变态", "https://baike.baidu.com/item/%E5%8F%98%E6%80%81/32741");
        NAME_TO_URL_MAP.put("不孝", "https://baike.baidu.com/item/%E4%B8%8D%E5%AD%9D?fromModule=lemma_search-box");
        NAME_TO_URL_MAP.put("不义", "https://baike.baidu.com/item/%E4%B8%8D%E4%B9%89?fromModule=lemma_search-box");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_contact_detail);

        textView = findViewById(R.id.textView_contact_detail);
        textView2=findViewById(R.id.textView2);

        // 从启动该Activity的Intent中获取名为"name"的额外数据
        String name = getIntent().getStringExtra("name");

        if (name != null) {
            // 构造显示文本并设置给textView
            String str = "这是" + name + "的详情页面";
            textView.setText(str);

            // 从Map中获取name对应的URL
            String url = NAME_TO_URL_MAP.get(name);
            if (url != null) {// 为textView2设置点击事件监听器，点击时打开URL
                setClickListenerWithUrl(url);
            } else {}
        }
    }

    // 定义一个私有方法，用于为textView2设置点击事件监听器
    private void setClickListenerWithUrl(final String url) {
        // 为textView2设置点击事件监听器
        textView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个Intent，用于打开浏览器并访问URL
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));// 设置Intent的数据为URL
                startActivity(intent);// 启动Activity，这里会打开浏览器并加载URL
            }
        });
    }
}
